import '@/apis/interceptors'
import axios from 'axios'

import { message } from "antd";
// import storage from '@/utils/Storage'
// import { message } from '@/utils/message'
// import { countStore } from '@/store/modules/count/index'

// import {
//     Message
//     // MessageBox
// } from 'element-ui'
// import router from '@/router/index'
// const { VITE_BASE_URL } = import.meta.env;

const http = {
    // get请求
    async get(path, params, head, showMsg = true) {
        let config = { path, params, head }
        config.method = 'get'
        config.showMsg = showMsg

        const awaitGet = await http.request(config)
        return awaitGet
    },
    // post请求
    async post(path, params, head, showMsg = true) {
        let config = { path, params, head }
        config.method = 'post'
        config.showMsg = showMsg
        // config.baseURL = import.meta.env[params.webBaseUrl || 'VITE_BASE_URL']
        // config.baseURL = baseUrl
        const awaitPost = await http.request(config)
        return awaitPost
    },
    async $post(path, params, head, showMsg = true) {
        let config = { path, params, head }
        config.method = 'post'
        config.showMsg = showMsg
        config.data = params
        config.head = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        // config.baseURL = import.meta.env[params.webBaseUrl || 'VITE_BASE_URL']
        // config.baseURL = baseUrl
        const awaitPost = await http.request(config)
        return awaitPost
    },
    // putt请求
    async put(path, params, head, showMsg = true) {
        let config = { path, params, head }
        config.method = 'put'
        config.showMsg = showMsg
        // config.baseURL = import.meta.env[params.webBaseUrl || 'VITE_BASE_URL']
        const awaitPut = await http.request(config)
        return awaitPut
    },
    // del请求
    async delete(path, params, head, showMsg = true) {
        let config = { path, params, head }
        config.method = 'delete'
        config.showMsg = showMsg
        const awaitDel = await http.request(config)
        return awaitDel
    },
    // post请求
    async upload(path, params, head, showMsg = true) {
        let config = { path, params, head }
        config.showMsg = showMsg
        config.headers = {
            'Content-Type': 'multipart/form-data'
        }
        config.method = 'post'
        // config.baseURL = import.meta.env[params.webBaseUrl || 'VITE_BASE_URL']
        const awaitPost = await http.request(config)
        return awaitPost
    },
    // blob下载流
    async blobDownload(path, params, head, showMsg = true) {
        let config = { path, params, head }
        config.headers = {
            'Content-Type': 'application/octet-stream;charset=UTF-8'
        }
        config.responseType = 'blob'
        config.method = 'get'
        config.showMsg = showMsg
        config.baseURL = import.meta.env.VITE_BASE_URL
        const awaitPost = await http.request(config)
        return awaitPost
    },
    // 发起请求
    request(config) {
        let { path, method, head, params } = config
        const [messageApi, contextHolder] = message.useMessage();
        // const nodeEnv = process.env.NODE_ENV
        // const prefix = (nodeEnv === 'production') ? '/api' : '/api'
        // const prefix =  
        config.baseURL = import.meta.env.VITE_BASE_URL
        // config.baseURL = 'https://ccy-agent-admin-test.ccynice.com/admin-api/'
        // config.baseURL = import.meta.env[params.webBaseUrl || 'VITE_BASE_URL']
        config.headers = { ...config.headers, ...head }
        // 'Device-Id': 1,
        // 'Client-Type': 'SHOP_CONSOLE',

        // config.url =  `${path}`
        config.url = `${path}`
        if (method === 'post' || method === 'put') {
            config.data = params
            delete config.params
        }

        return new Promise(async resolve => {
            try {

                const ret = await axios(config)

                // debugger
                if (ret.config?.responseType === 'blob') {
                    if (Number(ret.status) === 200 || Number(ret.status) === 300) {
                        ret.data.code = Number(ret.data.code)
                        resolve(ret)
                    }
                } else {
                    if (Number(ret.data.code === 200)) {
                        // 结果处理
                        resolve(ret.data)
                    } else {
                        // 当查询成功,并且不是get请求才显示消息提示
                        if (![200, 401].includes(ret.data.code) && config.showMsg) {
                            // ElMessage.error(ret.data.msg || '操作异常')
                            // Message({ type: 'error', message: ret.data.msg || '操作异常' })
                            messageApi.error(ret.data.msg || '操作异常');
                        }

                        ret.data.code = Number(ret.data.data)
                        // 结果处理
                        resolve(ret.data)
                    }
                }
                // const useCountStore = countStore()
                // console.log('---', new Date().getTime(), http.status)
                // http.status = false
                // utils.shake(useCountStore.countApi())

            } catch (err) {

            }
        })
    }
}

export default http
